//window对象里滚动条方法的兼容
function scroll() {
  if (window.pageYOffset) {
    return {
      top: window.pageYOffset,
      left: window.pageXOffset,
    };
  }

  if (document.compatMode == "CSS1Compat") {
    return {
      top: document.documentElement.scrollTop,
      left: document.documentElement.scrollLeft,
    };
  }

  return {
    top: document.body.scrollTop,
    left: document.body.scrollLeft,
  };
}

//client兼容
function client() {
  // ie兼容其他浏览器
  if (window.innerWidth != null) {
    return {
      width: window.innerWidth,
      height: window.innerHeight,
    };
  }
  // 标准浏览器兼容
  if ((document.compatMode = "CSS1Compat")) {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
    };
  }

  // 其他浏览器兼容
  return {
    width: document.body.clientWidth,
    height: document.body.clientHeight,
  };
}

//封装兼容ie和标准浏览器获取样式的方法
function getStyle(obj, attr) {
  if (obj.currentStyle) {
    return obj.currentStyle[attr];
  } else {
    return window.getComputedStyle(obj, null)[attr];
  }
}

//动态效果
function animate(obj, json, callback) {
  clearInterval(obj.clearA); //默认清除一次定时器
  obj.clearA = setInterval(function () {
    //给传入对象里加一个定时器
    var flag = true; //判断属性对象是否执行完毕
    //遍历传入的对象
    for (var attr in json) {
      //获取样式  获取动态效果的初始值
      if (attr == "opacity") {
        var current = Math.round(getStyle(obj, attr) * 100) || 0;
      } else {
        var current = parseInt(getStyle(obj, attr)); // w:100  h:100  l:0  t:50/200
      }

      //步长        等同于target- leader
      var leader = (json[attr] - current) / 10;
      leader = leader > 0 ? Math.ceil(leader) : Math.floor(leader);

      if (attr == "opacity") {
        if ("opacity" in obj.style) {
          obj.style.opacity = (current + leader) / 100;
        } else {
          obj.style.filter = "alpha(opacity," + (current + leader) + ")";
        }
      } else if (attr == "zIndex") {
        obj.style.zIndex = json[attr];
      } else {
        obj.style[attr] = current + leader + "px";
      }

      if (current != json[attr]) {
        flag = false;
      }
    }

    if (flag) {
      clearInterval(obj.clearA);
      if (callback) {
        callback(); //回调函数
      }
    }
  }, 15);
}
